import { createRouter, createWebHashHistory, createWebHistory, RouteRecordRaw } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    redirect:'goods',
    children:[
      {
        path:'goods',
        name:'goods',
        meta:{
          isShow:true,
          title:'商品列表'
        },
        component:()=>import('../views/GoodsView.vue')
      },
      {
        path:'user',
        name:'user',
        meta:{
          isShow:true,
          title:'用户列表'
        },
        component:()=>import('../views/UserView.vue')
      },
      {
        path:'role',
        name:'role',
        meta:{
          isShow:true,
          title:'角色列表'
        },
        component:()=>import('../views/RoleView.vue')
      },
      {
        path:'authority',
        name:'authority',
        meta:{
          isShow:false,
          title:'权限列表'
        },
        component:()=>import('../views/AuthorityView.vue')
      },
      {
        path:'table',
        name:'table',
        meta:{
          isShow:true,
          title:'表格'
        },
        component:()=>import('../views/TableView.vue')
      },
    ]
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
    path: '/login',
    name: 'login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/LoginView.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(process.env.BASE_URL),
  routes
})
// 路由守卫
router.beforeEach((to, from,next) => {
  // ...
  // 返回 false 以取消导航
  const token:string|null = localStorage.getItem('token')
  if(!token&&to.path!=='/login') {
    console.log(123);
    next('/login')
  }else{
    next()
  }
  // return false
})

export default router
